#include<iostream>
#include<mysql/mysql.h>
#include<jsoncpp/json/json.h>
#include<stdlib.h>
#include<unistd.h>
#include"common.hpp"
using namespace std;
/*
    删除博客cgi
*/
int mysql_detel(const string&query,const string&token)
{
    string userid;
    stringstream ss;
    if(!AuthMiddleware::verify_token(token,userid))return -1;
    MYSQL *conn = mysql_init(nullptr);
    if (nullptr == mysql_real_connect(conn, "127.0.0.1", "ldcwd", "123456", "httptest", 3306, nullptr, 0))
    {
        //std::cerr << " mysql connect error !!" << std::endl;
        return -2;
    }
    //std::cerr << "sql connect success!! " << std::endl;
    ss<<"delete from blogs where user_id="<<userid<<" and id="<<query<<";";
    //cerr<<"DEBUG: SQL: "<<ss.str()<<endl;
    if(mysql_query(conn,ss.str().c_str())!=0)return -3;
    //cerr<<"DEBUG-------------------------1-------";
    return 0;
}

int main()
{
    string query_string;
    query_string=getenv("QUERY_STRING");
    //cerr<<"DEBUG:  "<<query_string<<endl;
    string token;
    token =getenv("Authorization");
    if(!getToken(token))return -1;
   int n= mysql_detel(query_string,token);
   Json::Value v;
   if(n==0)
   {
    v["message"]="删除成功";
    v["success"]=true;
   }
   else
   {
    
    v["message"]="删除失败";
    v["success"]=false;
   }
   Json::StyledWriter w;
   string ret=w.write(v);
   cout<<ret;
    return 0;
}